﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class Danhsachthuyetminh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                hien();
        }

        protected void hien()
        {
            using (DataTable THONGTINTHUYETMINHDT = getThongtinTMDT())
            {
                grvThongtinTM.DataSource = THONGTINTHUYETMINHDT;
                grvThongtinTM.DataBind();
            }
        }
        DataTable getThongtinTMDT()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Select_ThongtinTM", Cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable("THONGTINTHUYETMINHDT");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }
        protected void grvThongtinTM_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int thuyetminhID = Convert.ToInt32(e.CommandArgument);

                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Xoathuyetminh", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("@MaTM", thuyetminhID);

                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hien();
                    }
                }
            }
        }
        protected void delete_linhvuc(int TMID)
        {
            // if (e.CommandName.ToLower().Equals("xoa"))
            //{
            // int chuyenmucID = Convert.ToInt32(e.CommandArgument);
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Xoathuyetminh", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaTM", TMID);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();

                }
            }
            // }
        }
        protected void ibtXoa_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in grvThongtinTM.Rows)
            {
                CheckBox cb = r.FindControl("chkchon") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    int lvucID = Convert.ToInt32(grvThongtinTM.DataKeys[r.RowIndex].Value);
                    delete_linhvuc(lvucID);
                }
            }
            hien();
        }
        protected void grvThongtinTM_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvThongtinTM.PageIndex = e.NewPageIndex;
            hien();

        }

    }
}